package controllers;

import play.*;
import play.data.validation.Required;
import play.data.validation.Validation;
import play.mvc.*;

import java.util.*;

import org.apache.commons.mail.EmailException;

import models.*;

/**
 * comments controller created to handle the methods of comments posted on idea.
 * 
 * @author Nada Hassan
 * 
 */
public class Comments extends Controller {

	public static void index() {
		render();
	}

	/**
	 * @param ideaId1
	 *            id of the idea
	 * @author Nada Hassan
	 */
	public static Long ideaId1;

	/**
	 * Post method creates an object of type comment and then add it to the idea
	 * that is created and then added to the list of comments for this specific
	 * idea and then render it and then show all the comments attached to an
	 * idea.
	 * 
	 * @param ideaId
	 *            the id of the idea.
	 * @param string
	 *            body body of the comment.
	 * @Author Nada Hassan
	 */

	public static void Post(Long ideaId1) {
		String user_id = session.get("userId");
		Long id1 = Long.parseLong(user_id);
		User commentator = User.findById(id1);
		// System.out.println("IDDDDD:" + ideaId);
		Idea i = Idea.findById(ideaId1);
		if (validation.hasErrors()) {
			flash.error("Please submit your comment first!");
			PostingIdea.showUserIdea(ideaId1);
		}
	String	body = params.get("body");
		Comment comment = new Comment(i, commentator, body);
		comment.save();
		i.comments.add(comment);
		i.save();
		PostingIdea.showUserIdea(ideaId1);
	}

	/**
	 * takes a comment id, retrieves the comment and the User that marked the
	 * comment as spamed and will set the comment to spamed using the
	 * spamComment method from the model.
	 * 
	 * @param id
	 *            The topic id that got spammed
	 * 
	 * @author Shahir El Faramawy
	 */
	public static void showCommentSpam(Long id) throws EmailException {
		String temp = session.get("userId");
		if (temp == null) {
			Home.home(null, null, "Please Login");
		}
		Long idTemp = Long.parseLong(temp);
		User spamer = User.findById(idTemp);
		Comment spammed = Comment.findById(id);
		spammed.spamComment(spamer);
		// showTopic(id);

	}

	/**
	 * Delete comment method delete the comment on idea based on the delete
	 * button clicked on the idea page and then the method renders a page that
	 * shows a message that the comment has been successfully deleted.
	 * 
	 * @param commentId
	 *            id of the comment to be deleted.
	 * @param ideaId1
	 *            id of the idea in which the comment is added to it
	 * @author Nada Hassan
	 */
	public static void deleteComment(Long commentId, Long ideaId1) {
		Idea i = Idea.findById(ideaId1);
		Comment comment = Comment.findById(commentId);
		i.comments.remove(comment);
		comment.commentator = null;
		comment.idea = null;
		comment.save();
		comment.delete();
		// // Comments.Post();
		PostingIdea.showUserIdea(ideaId1);
	}
	/**
	 * postOnIdea method creates an object of type comment and then add it to the idea
	 * that is created and then added to the list of comments for this specific
	 * idea and then render it and then show all the comments attached to an
	 * idea.
	 * 
	 * @param ideaId
	 *            the id of the idea.
	 * @param string
	 *            body body of the comment.
	 * @Author Nada Hassan
	 */

	public static void postOnIdea(Long ideaId1,@Required String body) {
		String user_id = session.get("userId");
		Long id1 = Long.parseLong(user_id);
		User commentator = User.findById(id1);
		// System.out.println("IDDDDD:" + ideaId);
		Idea i = Idea.findById(ideaId1);
		if (validation.hasErrors()) {
			flash.error("Please submit your comment first!");
			PostingIdea.showUserIdea(ideaId1);
		}
	    // String	body = params.get("body");
		Comment comment = new Comment(i, commentator, body);
		comment.save();
		i.comments.add(comment);
		i.save();
		PostingIdea.ShowIdea(ideaId1);
	}

	/**
	 * deleteCommentOnIdea method delete the comment on idea based on the delete
	 * button clicked on the showIdea page and then the method renders a page that
	 * shows a message that the comment has been successfully deleted.
	 * 
	 * @param commentId
	 *            id of the comment to be deleted.
	 * @param ideaId1
	 *            id of the idea in which the comment is added to it
	 * @author Nada Hassan
	 */
	public static void deleteCommentOnIdea(Long commentId, Long ideaId1) {
		Idea i = Idea.findById(ideaId1);
		Comment comment = Comment.findById(commentId);
		i.comments.remove(comment);
		comment.commentator = null;
		comment.idea = null;
		comment.save();
		comment.delete();
		// // Comments.Post();
		PostingIdea.ShowIdea(ideaId1);
	}
	/**
	 * postOnIdeaforOrganzier method creates an object of type comment and then add it to the idea
	 * that is created and then added to the list of comments for this specific
	 * idea and then render it and then show all the comments attached to an
	 * idea.
	 * 
	 * @param ideaId
	 *            the id of the idea.
	 * @param string
	 *            body body of the comment.
	 * @Author Nada Hassan
	 */
	
	
	public static void postOnIdeaForOgranizer(Long ideaId1, @Required String body) {
		String user_id = session.get("userId");
		Long id1 = Long.parseLong(user_id);
		User commentator = User.findById(id1);
		// System.out.println("IDDDDD:" + ideaId);
		Idea i = Idea.findById(ideaId1);
		if (validation.hasErrors()) {
			flash.error("Please submit your comment first!");
			PostingIdea.showUserIdea(ideaId1);
		}
	   // String	body = params.get("body");
		Comment comment = new Comment(i, commentator, body);
		comment.save();
		i.comments.add(comment);
		i.save();
		PostingIdea.showIdeaForOrganizer(ideaId1);
	}
	/**
	 * deleteCommentForIdeaOrganizer method delete the comment on idea based on the delete
	 * button clicked on the showideaforOrganizerPage page.
	 * 
	 * @param commentId
	 *            id of the comment to be deleted.
	 * @param ideaId1
	 *            id of the idea in which the comment is added to it
	 * @author Nada Hassan
	 */
	
	public static void deleteCommentForIdeaOrganizer(Long commentId, Long ideaId1) {
		Idea i = Idea.findById(ideaId1);
		Comment comment = Comment.findById(commentId);
		i.comments.remove(comment);
		comment.commentator = null;
		comment.idea = null;
		comment.save();
		comment.delete();
		// // Comments.Post();
		PostingIdea.showIdeaForOrganizer(ideaId1);
	}

}
